#include "FileInfo.h"
#include <iostream>
#include <algorithm>

Utilities::FileInfo::FileInfo(const char* filename)
{
	this->flag_started	= new short(0);
	this->exists		= new bool(0);
	this->modified		= new bool(0);
	this->size			= new long(0);
	this->timestamp		= new int(0);
	this->filename		= new char[256];
	this->filename		= filename;

	this->vFileExists();

	if(*this->exists)
	{
		this->DefaultProccs();
	}

	*this->flag_started = 1;
	std::cout << "FileInfo for " << this->filename << " initialized." << std::endl;
}

void Utilities::FileInfo::DefaultProccs()
{
	this->vGetsModifiedTime();
}

Utilities::FileInfo::~FileInfo(void)
{
	std::cout << "Deleting Inforation for file " << this->filename << std::endl;
	delete	this->filename,
			this->flag_started,
			this->exists,
			this->modified,
			this->size,
			this->timestamp;
}

void swap(Utilities::FileInfo& This, Utilities::FileInfo& FI) // no throw
{
	This.pSetFileName(*FI.pGetFileName());
	This.pSetExistsFlag(*FI.pGetExistsFlag());
	This.pSetModifiedFlag(*FI.pGetModifiedFlag());
	This.pSetTimeStamp(*FI.pGetTimeStamp());
	This.pSetSize(*FI.pGetSize());
}

////////////////////////////////////////////////////////////////////////////////////////

bool Utilities::FileInfo::vFileExists()
{
	long begin,end;

	std::ifstream f(this->filename);
	
	if(f.is_open())
	{
		begin = f.tellg();
		f.seekg (0, std::ios::end);
		end = f.tellg();
		f.close();
		*this->size = (end-begin);
		*this->exists = true;

		return true;
	}

	*this->exists = false;
	return false;
}

Utilities::FileInfo& Utilities::FileInfo::operator=(FileInfo& FI)
{
	swap(*this, FI);
	return *this;
}

const char*& Utilities::FileInfo::pGetFileName()
{
	return this->filename;
}

bool*& Utilities::FileInfo::pGetModifiedFlag()
{
	return this->modified;
}

bool*& Utilities::FileInfo::pGetExistsFlag()
{
	return this->exists;
}

long*& Utilities::FileInfo::pGetSize()
{
	return this->size;
}

int*& Utilities::FileInfo::pGetTimeStamp()
{
	return this->timestamp;
}

void Utilities::FileInfo::pSetFileName(const char &filename)
{
	this->filename = &filename;
}
void Utilities::FileInfo::pSetModifiedFlag(bool& modified)
{
	this->modified = &modified;
}
void Utilities::FileInfo::pSetExistsFlag(bool& exists)
{
	this->exists = &exists;
}
void Utilities::FileInfo::pSetSize(long& size)
{
	this->size = &size;
}
void Utilities::FileInfo::pSetTimeStamp(int& timestamp)
{
	this->timestamp = &timestamp;
}

void Utilities::FileInfo::showFileInfo()
{
	std::cout << "------------------------------------------------------------>" << std::endl;
	std::cout << "File Info for " << this->filename << std::endl;
	std::cout << "Exists       " << "\t" << (*this->exists? "true" : "false") << std::endl;
	
	if(*this->exists)
	{
		std::cout << "Size         " << "\t" << *this->size << " bytes" << std::endl;
		std::cout << "Modified Time" << "\t" << *this->timestamp << std::endl;
		std::cout << "Was Modified?" << "\t" << (*this->modified? "true" : "false") << std::endl;
	}
}

int Utilities::FileInfo::vGetsModifiedTime()
{
	time_t clock;
	int modftime;
	struct stat attrib;
	stat(this->filename,&attrib);
	modftime = attrib.st_mtime;
	if(*this->timestamp != modftime && *this->flag_started)
		*this->modified = true;
	*this->timestamp = modftime;

	return modftime;
}

void Utilities::FileInfo::ReloadFileInfo()
{
	std::cout << "------------------------------------------------------------>" << std::endl;
	std::cout << "Reloading file " << this->filename << std::endl;
	this->vFileExists();
	*this->modified = false;
	if(*this->exists)
	{
		this->DefaultProccs();
	}
}

const char* Utilities::FileInfo::FileName()
{
	return this->filename;
}

bool Utilities::FileInfo::Modified()
{
	return *this->modified;
}

bool Utilities::FileInfo::Exists()
{
	return *this->exists;
}
long Utilities::FileInfo::Size()
{
	return *this->size;
}
int Utilities::FileInfo::TimeStamp()
{
	return *this->timestamp;
}